﻿using System;
using System.Text.RegularExpressions;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.Framework.Common.Security;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Runtime;
using ZyGames.Framework.Game.Service;
using ZyGames.Framework.Game.Sns;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 10003_自定义手机号和密码
	/// </summary>
	/// <remarks>继续BaseStruct类:不检查用户合法性请求;AuthorizeAction:有验证合法性</remarks>
	public class Action10003 : BaseStruct
	{
		private int mobileType = 0;
		private int gameID = 0;
		private string retailID = string.Empty;
		private string clientAppVersion = string.Empty;
		private int ScreenX = 0;
		private int ScreenY = 0;
		private string Name = string.Empty;

		private bool result = false;
		private string log = string.Empty;

		private string PhoneNum = string.Empty;
		private string Pwd = string.Empty;

		public Action10003(HttpGet httpGet)
			: base((int)ActionType.DZRegist, httpGet)
		{
		}

		/// <summary>
		/// 客户端请求的参数较验
		/// </summary>
		/// <returns>false:中断后面的方式执行并返回Error</returns>
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("MobileType", ref mobileType) &&
				httpGet.GetInt("GameID", ref gameID) &&
				httpGet.GetString("RetailID", ref retailID) &&
				httpGet.GetString("ClientAppVersion", ref clientAppVersion) &&
				httpGet.GetString("PhoneNum", ref PhoneNum) &&
				httpGet.GetString("Pwd", ref Pwd))
			{
				httpGet.GetInt("ScreenX", ref ScreenX);
				httpGet.GetInt("ScreenY", ref ScreenY);
			}
			else
			{
				return false;
			}
			return true;
		}

		/// <summary>
		/// 业务逻辑处理
		/// </summary>
		/// <returns>false:中断后面的方式执行并返回Error</returns>
		public override bool TakeAction()
		{
			try
			{
				var length = Pwd.Length;
				if (length < 6 || length > 12)
				{
					result = false;
					log = "密码长度要在6-12个字符";
					return true;
				}

				Regex rx = new Regex(@"^[1][358][0-9]{9}$");
				if (!rx.IsMatch(PhoneNum))
				{
					result = false;
					log = "密码长度要在6-12个字符";
					return true;
				}

				var res = SnsManager.Register(PhoneNum, Pwd, "", true);  //在Scut\Source\Middleware\Sns\SnsManager  Line 68
				if (res)
				{
					result = GameTable.Current.AddPhoneAndPwdInfo(PhoneNum, Pwd);
					if (!result)
					{
						result = false;
						log = "该号码已经被注册,请更换号码";
						return true;
					}
				}
				else
				{
					result = false;
					log = "注册有误";
					return true;
				}

				log = "注册成功";
				return true;
			}
			catch (Exception ex)
			{
				this.SaveLog(ex);
				this.ErrorCode = Language.Instance.ErrorCode;
				this.ErrorInfo = Language.Instance.St1002_GetRegisterPassportIDError;
				return false;
			}
		}
		/// <summary>
		/// 下发给客户的包结构数据
		/// </summary>
		public override void BuildPacket()
		{
			PushIntoStack(result);
			PushIntoStack(log);
			PushIntoStack(PhoneNum);
			PushIntoStack(Pwd);
		}
	}
}
